package com.example.money.test.Async;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MyAsyncTask extends AsyncTask<String, Void,Bitmap> {
    private String TAG = "MyAsyncTask";

    private Handler handler;

    public MyAsyncTask(Handler handler){
        this.handler = handler;

    }


    @Override
    protected Bitmap doInBackground(String... strings) {
        String url = strings[0];//这是一个可边长度的参数，可以传入多个string
        URLConnection connection;
        Bitmap bitmap = null;
        InputStream is;
        try {
            connection = new URL(url).openConnection();
            is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bitmap = BitmapFactory.decodeStream(bis);
            is.close();
            bis.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    @Override
    protected void onPreExecute() {
        Log.e(TAG, "onPreExecute: ");
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        Message msg = new Message();
        msg.obj = bitmap;

        handler.sendMessage(msg);

    }
}
